# -*- codeing = utf-8 -*-
# @Time  :2021/6/29 14:12
# @Author:wangyuhan
# @File  :WindowShow.py
# @Software: PyCharm

import tkinter
import tkinter.ttk
import pandas as pd
import PIL.Image
import PIL.ImageTk


class MainForm():
    def __init__(self, datafile='./BaiduScholar/BaiduScholar.xlsx', image_path='./BaiduScholar/miku.png'):
        self.DataFile = datafile
        self.Image_path = image_path
        self.paper_dict = {}  # 文章内容
        self.root = tkinter.Tk()     # 创建窗体
        self.root.title('wangyuhan的程序')
        self.root.geometry("1200x290")  # 设置窗体尺寸
        self.creat_init_list() # 初始化列表
        self.load_data()      # 加载数据
        self.root.mainloop()   # 循环监听


    def creat_init_list(self):
        self.treeview = tkinter.ttk.Treeview(self.root, columns=('number', 'title', 'author', 'keyword', 'f', 'time', 'DOI', 'abstract'),
                                             show="headings", height=20)  # 创建普通列表
        # 配置列
        self.treeview.column('number', width=4, anchor=tkinter.CENTER)
        self.treeview.column('title', width=120, anchor=tkinter.W)   # anchor: 文字在cell里的对齐方式
        self.treeview.column('author', width=60, anchor=tkinter.W)
        self.treeview.column('keyword', width=60, anchor=tkinter.CENTER)
        self.treeview.column('f', width=5, anchor=tkinter.CENTER)
        self.treeview.column('time', width=10, anchor=tkinter.CENTER)
        self.treeview.column('DOI', width=30, anchor=tkinter.CENTER)
        self.treeview.column('abstract', width=220, anchor=tkinter.W)
        # 设置标题
        self.treeview.heading(column='number', text='序号')
        self.treeview.heading(column='title', text='标题')
        self.treeview.heading(column='author', text='作者')
        self.treeview.heading(column='keyword', text='关键词')
        self.treeview.heading(column='f', text='被引量')
        self.treeview.heading(column='time', text='发表年份')
        self.treeview.heading(column='DOI', text='DOI')
        self.treeview.heading(column='abstract', text='摘要')
        '''
        定义滚动条控件
        orient为滚动条的方向，vertical:纵向，horizontal:横向
        command = self.tree.yview   将滚动条绑定到treeview控件的Y轴
        '''
        self.scrollbar = tkinter.Scrollbar(self.root, orient='vertical', command=self.treeview.yview)
        # 滚动条显示
        self.scrollbar.pack(side=tkinter.RIGHT, fill=tkinter.Y)
        # 配置滚动条
        # self.scrollbar.config(command=)
        self.treeview.configure(yscrollcommand=self.scrollbar.set)
        self.treeview.bind("<Double-Button-1>", self.paper_item_show)  # 绑定事件
        self.treeview.pack(fill=tkinter.BOTH)  # 显示组件

    def load_data(self):   # 创建下拉列表框
        self.frame = tkinter.Frame(self.root)
        data = pd.read_excel(self.DataFile)
        len = data.shape[0]   # 返回data的行数
        for i in range(len):
            number = str(i+1)
            title = data.loc[i, 'title']
            author = data.loc[i, 'author']
            keyword = data.loc[i, 'keyword']
            f = data.loc[i, 'f']
            pub_time = int(data.loc[i, 'time']) if data.loc[i, 'time']>0 else data.loc[i, 'time']
            DOI = data.loc[i, 'DOI']
            abstract = data.loc[i, 'abstract']
            self.treeview.insert(parent='', index=tkinter.END,
                                 values=(number, title, author, keyword, f, pub_time, DOI, abstract))

    def paper_item_show(self, event):   # 显示文章详细信息
        for index in self.treeview.selection():
            values = self.treeview.item(index, "values")    # 获得选定内容
            # 规范年份信息，如将2016.0转换为2016
            self.subroot = tkinter.Toplevel()      # 创建子窗体
            self.subroot.title('wangyuhan的小窗口')      # 设置子窗体标题
            self.subroot.geometry('800x400')       # 设置子窗体尺寸
            # picture = PIL.ImageTk.PhotoImage(image=PIL.Image.open(Image_path), size='0.01x0.01')  # 加载图片
            picture = PIL.ImageTk.PhotoImage(image=PIL.Image.open(self.Image_path).resize((290, 380)))  # 加载图片
            label_picture = tkinter.Label(self.subroot, image=picture)
            label_picture.grid(row=0, column=0)    # 显示图片
            sub_frame = tkinter.Frame(self.subroot)  # 定义子Frame
            # 设置标签
            tkinter.Label(sub_frame, text='【标题】' + values[1], font=('微软雅黑', 12), justify='left').grid(row=0, sticky=tkinter.W)
            tkinter.Label(sub_frame, text='【作者】' + values[2], font=('微软雅黑', 12), justify='left').grid(row=1, sticky=tkinter.W)
            tkinter.Label(sub_frame, text='【关键词】' + values[3], font=('微软雅黑', 12), justify='left').grid(row=2, sticky=tkinter.W)
            tkinter.Label(sub_frame, text='【被引量】' + values[4], font=('微软雅黑', 12), justify='left').grid(row=3, sticky=tkinter.W)
            tkinter.Label(sub_frame, text='【年份】' + values[5], font=('微软雅黑', 12), justify='left').grid(row=4, sticky=tkinter.W)
            tkinter.Label(sub_frame, text='【DOI】' + values[6], font=('微软雅黑', 12), justify='left').grid(row=5, sticky=tkinter.W)
            tkinter.Label(sub_frame, text='【摘要】', font=('微软雅黑', 12), justify='left').grid(row=6, sticky=tkinter.W)
            # 创建一个frame
            abstract_frame = tkinter.Frame(sub_frame)
            abstract_listbox = tkinter.Listbox(abstract_frame, height=10, width=50)  # 创建Listbox

            # 需要设置34个中文字符换行
            Length = len(values[7].split('/'))
            list_item = list(values[7])
            text = ['']
            num = 0
            Index = 0
            for i in list_item:
                if (Index+1) % 30 == 0:
                    num += 1
                    text.append(i)
                else:
                    text[num] = text[num]+i
                Index += 1
            for item in text:
                abstract_listbox.insert(tkinter.END, item)

            abstract_scrollbar = tkinter.Scrollbar(abstract_frame)
            abstract_scrollbar.config(command=abstract_listbox.yview)
            abstract_scrollbar.pack(side=tkinter.RIGHT, fill=tkinter.Y)
            abstract_listbox.pack()

            abstract_frame.grid(row=7, sticky=tkinter.W)    # 显示Frame
            sub_frame.grid(row=0, column=1, sticky=tkinter.N)
            self.subroot.mainloop()    # 显示子窗体
